package cn.vivi.design.configfactory;

import cn.vivi.design.simpleFactory.Coffee;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.*;

public class CoffeeFactory {

    private static Map<String, Coffee> map = new HashMap<>();

    static {
        Properties prop = new Properties();
        try {
            InputStream is = new FileInputStream(new File("D:\\code\\ideac\\workspace\\demo\\src\\cn\\vivi\\design\\configfactory\\bean.properties"));
            prop.load(is);
            Set<Object> keySet = prop.keySet();
            for (Object key : keySet) {
                String coffeeClass = (String) prop.get(key);
                Class<?> coffee = Class.forName(coffeeClass);
                Coffee bean = (Coffee) coffee.newInstance();
                map.put((String) key, bean);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    public static Coffee createCoffee(String name) {
        return map.get(name);
    }
}
